﻿/*
=======================================================
    GroupInfo【集团】
=======================================================
*/

using Entities;
using static System.Net.WebRequestMethods;

namespace Services
{
    public interface IGroupInfoRepository : IRepository<GroupInfo>
    {
        int InsertGroupInfo(GroupInfo groupInfo, string loginName, string loginPassword);
        int UpdateIsDelete(bool flag, int[] ids);
        void ResetPassword(List<int> ids, string pwd);
    }


    public class GroupInfoRepository : BaseRepository<GroupInfo>, IGroupInfoRepository
    {
        public GroupInfoRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<GroupInfo> query)
            : base(dB, currentUser, query)
        {
        }

        #region 启用/禁用  UpdateIsDelete
        public int UpdateIsDelete(bool flag, int[] ids)
        {
            return _DB.Updateable<GroupInfo>()
                      .SetColumns(x => new GroupInfo { IsDelete = flag })
                      .Where(x => ids.Contains(x.GroupInfoID))
                      .ExecuteCommand();
        }
        #endregion

        #region GetPage
        public override (List<GroupInfo> items, int totalCount) GetPage(Expressionable<GroupInfo> where, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            var _query = _DB.Queryable<GroupInfo>().Where(where.ToExpression());

            if (orderBy.IsNotEmpty())
            {
                _query.OrderBy(orderBy);
            }

            int totalCount = 0;
            var list = _query.ToPageList(pageIndex, pageSize, ref totalCount);
            return (list, totalCount);
        }
        #endregion

        #region GetByID
        public override GroupInfo GetByID(int id)
        {
            var _query = _DB.Queryable<GroupInfo>();

            return _query.InSingle(id);
        }
        #endregion

        #region 添加集团  InsertGroupInfo
        public int InsertGroupInfo(GroupInfo groupInfo, string loginName, string loginPassword)
        {
            //Role,RolePermission,User,UserRole,Department,其它要插入的固定数据
            var now = DateTime.Now;

            //添加集团
            var groupInfoID = Insert(groupInfo);

            #region dbo的数据
            //添加角色
            var roleID = _DB.Insertable(new Role
            {
                GroupInfoID = groupInfoID,
                Name = "管理员",
                DataPermissionType = (int)Role_DataPermission.全部,
                IsDelete = false,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.角色, GroupInfoID = groupInfoID, DataID = roleID, DepartmentID = -1 }).ExecuteCommand();

            //获取权限
            var permissions = _DB.Queryable<Permission>().ToList();
            //添加角色与权限
            _DB.Insertable(permissions.Select(x => new RolePermission
            {
                GroupInfoID = groupInfoID,
                RoleID = roleID,
                PermissionID = x.PermissionID
            }).ToList()).ExecuteCommand();

            //添加用户
            var userID = _DB.Insertable(new User
            {
                GroupInfoID = groupInfoID,
                LoginName = loginName,
                LoginPassword = Gx.DesEncrypt(loginPassword),
                Name = "管理员",
                IsDelete = false,
                IsInit = true,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.用户, GroupInfoID = groupInfoID, DataID = userID, DepartmentID = -1 }).ExecuteCommand();

            //添加用户与角色
            _DB.Insertable(new UserRole
            {
                GroupInfoID = groupInfoID,
                RoleID = roleID,
                UserID = userID
            }).ExecuteCommand();

            //添加部门
            var departmentID = _DB.Insertable(new Department
            {
                GroupInfoID = groupInfoID,
                LeaderID = userID,
                LeaderName = "管理员",
                Name = groupInfo.Name,
                Type = (int)Department_Type.集团,
                ParentID = 0,
                IsDelete = false,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.部门, GroupInfoID = groupInfoID, DataID = departmentID, DepartmentID = -1 }).ExecuteCommand();
            #endregion


            //增加数据归属的模板代码
            //_DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.xxx, GroupInfoID = groupInfoID, DataID = xxx, DepartmentID = -1 }).ExecuteCommand();


            int insertID = 0;

            #region 基础数据与字典
            //状态标准
            insertID = _DB.Insertable(new StandStatus { GroupInfoID = groupInfoID, Name = "LA", Type = (int)StandStatus_Type.LA }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.状态标准, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();

            insertID = _DB.Insertable(new StandStatus { GroupInfoID = groupInfoID, Name = "DIA", Type = (int)StandStatus_Type.DIA }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.状态标准, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();

            insertID = _DB.Insertable(new StandStatus { GroupInfoID = groupInfoID, Name = "LMA", Type = (int)StandStatus_Type.LMA }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.状态标准, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();

            insertID = _DB.Insertable(new StandStatus { GroupInfoID = groupInfoID, Name = "综合", Type = (int)StandStatus_Type.综合 }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.状态标准, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();

            insertID = _DB.Insertable(new StandStatus { GroupInfoID = groupInfoID, Name = "捻距变化", Type = (int)StandStatus_Type.捻距变化 }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.状态标准, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();


            //提升机
            insertID = _DB.Insertable(new Hoister
            {
                GroupInfoID = groupInfoID,
                Code = "TSJ001",
                Name = "落地摩擦式JKMD",
                Pic_Imgs = "[{\"FileName\":\"c1.jpg\",\"Url\":\"http://qiniu-zyzz-demo.zyzztop.com/05-1702624306350.jpg\"}]",
                IsDelete = false,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.提升机管理, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();

            insertID = _DB.Insertable(new Hoister
            {
                GroupInfoID = groupInfoID,
                Code = "TSJ002",
                Name = "井塔摩擦式JKM",
                Pic_Imgs = "[{\"FileName\":\"c2.jpg\",\"Url\":\"http://qiniu-zyzz-demo.zyzztop.com/06-1702622883524.jpg\"}]",
                IsDelete = false,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.提升机管理, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();

            insertID = _DB.Insertable(new Hoister
            {
                GroupInfoID = groupInfoID,
                Code = "TSJ003",
                Name = "架空乘人装置",
                Pic_Imgs = "[{\"FileName\":\"c3.jpg\",\"Url\":\"http://qiniu-zyzz-demo.zyzztop.com/01-1702620016627.jpg\"}]",
                IsDelete = false,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.提升机管理, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();
            insertID = _DB.Insertable(new Hoister
            {
                GroupInfoID = groupInfoID,
                Code = "TSJ004",
                Name = "立井单箕斗缠绕提升",
                Pic_Imgs = "[{\"FileName\":\"c4.jpg\",\"Url\":\"http://qiniu-zyzz-demo.zyzztop.com/04-1702619486349.jpg\"}]",
                IsDelete = false,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.提升机管理, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();
            insertID = _DB.Insertable(new Hoister
            {
                GroupInfoID = groupInfoID,
                Code = "TSJ005",
                Name = "立井双箕斗缠绕提升",
                Pic_Imgs = "[{\"FileName\":\"c5.jpg\",\"Url\":\"http://qiniu-zyzz-demo.zyzztop.com/03-1702616338373.jpg\"}]",
                IsDelete = false,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.提升机管理, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();
            insertID = _DB.Insertable(new Hoister
            {
                GroupInfoID = groupInfoID,
                Code = "TSJ006",
                Name = "斜井缠绕式提升",
                Pic_Imgs = "[{\"FileName\":\"c6.jpg\",\"Url\":\"http://qiniu-zyzz-demo.zyzztop.com/02-1702535659561.jpg\"}]",
                IsDelete = false,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            _DB.Insertable(new DataDepartment { Type = (int)DataDepartment_Type.提升机管理, GroupInfoID = groupInfoID, DataID = insertID, DepartmentID = -1 }).ExecuteCommand();


            //出库类型
            _DB.Insertable(new OperType
            {
                GroupInfoID = groupInfoID,
                InOutType = (int)Bill_InOutType.出库,
                Name = "领料出库",
                IsInit = true,
                IsDelete = false,
                CreateUserID = 0,
                CreateUserName = "系统自动",
                CreateDate = now,
                UpdateUserID = 0,
                UpdateUserName = "系统自动",
                UpdateDate = now
            }).ExecuteReturnIdentity();
            #endregion


            return groupInfoID;
        }
        #endregion

        #region 重置密码  ResetPassword
        public void ResetPassword(List<int> ids, string pwd)
        {
            var userIDs = _DB.Queryable<User>().Where(x => ids.Contains(x.GroupInfoID) && x.IsInit == true).Select(x => x.UserID).ToList();

            _DB.Updateable<User>().SetColumns(x => x.LoginPassword == pwd)
                .Where(x => userIDs.Contains(x.UserID))
                .ExecuteCommand();
        }
        #endregion
    }
}
